package com.itheima.design.mode.strategy.orderHandle;

import com.itheima.design.mode.strategy.service.PromotionCalculation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
* 根据订单类型返回对应的处理策略
 */
@Component
public class HandlerPromotionContext {

    @Autowired
    ApplicationContext applicationContext;

    //存放所有策略类Bean的map
    public static Map<String, Class<PromotionCalculation>> promotionCalculationBeanMap= new HashMap<>();

    public PromotionCalculation getPromotionCalculation(String promotion) {
        Class<PromotionCalculation> promotionCalculationClass = promotionCalculationBeanMap.get(promotion);
        if(promotionCalculationClass == null) throw new IllegalArgumentException("没有对应的促销类型");
        //从容器中获取对应的策略Bean
        return applicationContext.getBean(promotionCalculationClass);
    }
}
